package com.example.administrator.wallet.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.constant.Constant;
import com.example.administrator.wallet.model.bean.BTCFragmentDataBean;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.administrator.wallet.view.activity.find.cointrans.CoinTransactionActivity;
import com.example.administrator.wallet.view.activity.find.cointrans.CoinTransactionDetailsActivity;

import java.util.List;


/**
 * Created by Administrator on 2018/8/24 0024.
 */

public class CoinTransactionActivityListviewAdapter extends BaseAdapter {

    private Context context;
    private List<BTCFragmentDataBean> list;
    private CoinTransactionAdapterViewHolder holder;
    private String upOrDownPercent;
    private double usdToCny;

    public CoinTransactionActivityListviewAdapter(Context context, List<BTCFragmentDataBean> list) {
        this.context = context;
        this.list = list;

        this.usdToCny = SharedPrefsUtil.getValue((CoinTransactionActivity)context,"wallet","usdToCny", (float) 0.0);
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            holder = new CoinTransactionAdapterViewHolder();
            convertView = View.inflate(context, R.layout.item_quotation_list, null);

            holder.titleMaster = (TextView) convertView.findViewById(R.id.title_master);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.tv_price_item_quotation_list);
            holder.tvChinesePrice = (TextView) convertView.findViewById(R.id.tv_chinese_price_item_quotation_list);
            holder.btnUpOrDown = (Button) convertView.findViewById(R.id.btn_up_or_down_item_quotation_list);

            convertView.setTag(holder);
        } else {
            holder = (CoinTransactionAdapterViewHolder) convertView.getTag();
        }



        final String title = list.get(position).getList().getStock() + "/" + list.get(position).getList().getMoney();
        final String price = getPrice(list.get(position).getPrice());

        double chinesePrice = 0.0;
        if (!list.get(position).getPrice().isEmpty()){
            chinesePrice = Double.parseDouble(list.get(position).getPrice()) * usdToCny;
        }

        holder.tvChinesePrice.setText(getPrice(chinesePrice + ""));

        double upOrDown = 0.0;
        try {
            upOrDown = list.get(position).getUpsOrDowns() * 100;

            holder.titleMaster.setText(title);

            holder.tvPrice.setText(price);

            String strUPOrDown = String.format("%.2f", upOrDown);

            if (list.get(position).getUpsOrDowns() < 0) {
                upOrDownPercent = strUPOrDown + "%";
                holder.btnUpOrDown.setText(upOrDownPercent);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        holder.btnUpOrDown.setBackground(context.getDrawable(R.drawable.quotation_but_jian));
                    }
                }
            } else {
                upOrDownPercent = "+" + strUPOrDown + "%";
                holder.btnUpOrDown.setText(upOrDownPercent);
            }

        } catch (Exception e) {

        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, CoinTransactionDetailsActivity.class);
                intent.putExtra(Constant.TO_COIN_TRANSACTION_DETAILS_TITLE, title);
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    private String getPrice(String price) {
        if (price == "") {
            return "";
        }

        if (!price.contains(".")){
            return price;
        }

        int index = price.indexOf(".");

        if (price.length() >= index + 3){
            String newPrice = price.substring(0, index + 3);
            return newPrice;
        } else {
            return price;
        }
    }

    public static class CoinTransactionAdapterViewHolder {
        TextView titleMaster, tvPrice, tvChinesePrice;
        Button btnUpOrDown;
    }


}
